package box.mantianx.config;

import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

/**
 * @author starrysky
 * @title: MyConfiguration
 * @projectName spring_ma
 * @description: TODO
 * @date 2021/2/317:04
 */
@Configuration
@ImportResource("classpath:applicationContext.xml")
public class MyConfiguration {

    private final DriverManagerDataSource dataSource = new DriverManagerDataSource();
    private final SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean();
    private final PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
    @Bean(name = "datasource")
    public DriverManagerDataSource dataSource() {
        dataSource.setDriverClassName("org.mariadb.jdbc.Driver");
        dataSource.setUrl("jdbc:mariadb://127.0.0.1:3306/spring_ma?useSSL=false&amp;useUnicode=true&amp;characterEncoding=utf-8&amp;serverTimezone=Asia/Shanghai");
        dataSource.setUsername("root");
        dataSource.setPassword("passwd");
        return dataSource;
    }

    @Bean("sqlSessionFactory")
    public SqlSessionFactoryBean sqlSessionFactoryBean() {
        sqlSessionFactory.setDataSource(dataSource);
        sqlSessionFactory.setConfigLocation(resolver.getResource("classpath:mybatis-config.xml"));
        sqlSessionFactory.setTypeAliasesPackage("box.mantianx.pojo");
        sqlSessionFactory.setMapperLocations(resolver.getResource("classpath:box/mantianx/dao/UserMapper.xml"));
        return sqlSessionFactory;
    }


}
